package com.common.dao.impl;

import java.util.List;

import com.common.dao.BiotechDAO;
import com.common.model.Biotech;


public class BiotechDaoimpl extends BaseDAO implements BiotechDAO {

	@Override
	public boolean save(Biotech biotech) {
		return saveOrUpdate(biotech);
	}

	@Override
	public boolean del(Biotech biotech) {
		this.delete(biotech);
		return true;
	}
	
	@Override
	public boolean update(Biotech biotech) {
		return saveOrUpdate(biotech);
	}


	@Override
	public Biotech load(int id) {
		StringBuffer sb = new StringBuffer("FROM Biotech WHERE id = ")
		.append(id);
		
		List<Biotech> lst = findByHQL(sb.toString());
		if(lst.size()>0){
			return lst.get(0);
		}else{
			return null;
		}
	}
	
	@Override
	public List<Biotech> list(Biotech biotech) {
		StringBuffer sb;
		sb = new StringBuffer("FROM Biotech where    repairman ='").append(biotech.getRepairman()).append("' order by pubdate desc");
		List<Biotech> lst = findByHQL(sb.toString());
		 return lst;
	}
	
	@Override
	public List<Biotech> listrepair(String repairman) {
		StringBuffer sb;
		sb = new StringBuffer("FROM Biotech where    repairman ='").append(repairman).append("' order by pubdate desc");
		List<Biotech> lst = findByHQL(sb.toString());
		 return lst;
	}
	
	@Override
	public boolean pai(Biotech biotech) {
		StringBuffer sb;
		sb = new StringBuffer("update Biotech set status = 1,repairman = '")
		.append(biotech.getRepairman()).append("',").append("price='").append(biotech.getPrice()).append("' where id=").append(biotech.getId());
		
		try {
			executeHql(sb.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return true;
	}
	
	@Override
	public List<Biotech> list0(String author) {
		StringBuffer sb;
		sb = new StringBuffer("FROM Biotech where    author ='").append(author).append("' order by pubdate desc");
		List<Biotech> lst = findByHQL(sb.toString());
		 return lst;
	}
	
	@Override
	public List<Biotech> yixiu(String author) {
		StringBuffer sb;
		sb = new StringBuffer("FROM Biotech where    author ='").append(author).append("' and status=3 order by pubdate desc");
		List<Biotech> lst = findByHQL(sb.toString());
		 return lst;
	}
	
	
	@Override
	public List<Biotech> listrepair0(Biotech biotech) {
		StringBuffer sb;
		sb = new StringBuffer("FROM Biotech where    type ='").append(biotech.getType()).append("' and status=0 order by pubdate desc");
		List<Biotech> lst = findByHQL(sb.toString());
		 return lst;
	}

	@Override
	public List<Biotech> listrepair1(String repairman) {
		StringBuffer sb;
		sb = new StringBuffer("FROM Biotech where    repairman ='").append(repairman).append("' and status in (2,3) order by pubdate desc");
		List<Biotech> lst = findByHQL(sb.toString());
		 return lst;
	}
	
	@Override
	public boolean updatestatus(int status, Biotech biotech) {
		StringBuffer sb;
		sb = new StringBuffer("update Biotech set status = ").append(status).append("  where id = ").append(biotech.getId());
		try {
			executeHql(sb.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return true;
	}
	


	@Override
	public boolean shenhe(Biotech biotech) {
		StringBuffer sb;
		sb = new StringBuffer("update Biotech set status = 1  where id = ").append(biotech.getId());
		
		try {
			executeHql(sb.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return true;
	}

	@Override
	public List<Biotech> listall(String name) {
		StringBuffer sb;
		if(name !=null && !name.equals("")){
			 sb = new StringBuffer("FROM Biotech WHERE  title like '%")
			.append(name).append("%'");
		}else{
			 sb = new StringBuffer("FROM Biotech WHERE status =1");
		}
		
		List<Biotech> lst = findByHQL(sb.toString());
		 return lst;
	}

	@Override
	public List<Biotech> list3() {
		StringBuffer sb;
		sb = new StringBuffer("FROM Biotech order by id desc");
		List<Biotech> lst = findByHQL(sb.toString());
		 return lst;
	}
}
